package date;

import org.junit.Test;

import java.time.*;

/**
 * @description: jdk对日期处理新增加的api
 * @author: apple
 * @date:  2021/12/11 7:36 下午
 * @version 1.0
 **/
public class LocalTimeTest {

    @Test
    public void test1() {
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        LocalDateTime of = LocalDateTime.of(2022, 1, 1, 1, 1, 1);
        System.out.println(of);

        // get
        System.out.println(localDateTime.getYear());
        System.out.println(localDateTime.getMonth().getValue());
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek().getValue());
        System.out.println(localDateTime.getHour());
        System.out.println(localDateTime.getMinute());
        System.out.println(localDateTime.getSecond());

        // set -> 体现不可变性
        LocalDate localDate1 = localDate.withDayOfMonth(20).withMonth(1);
        System.out.println(localDate1); // 现在的
        System.out.println(localDate); // 以前的

        // add
        LocalDateTime localDateTime1 = localDateTime.plusDays(4).plusMonths(1);
        System.out.println(localDateTime1);
        System.out.println(localDateTime);
    }
    
    @Test
    public void test2() {
        // 获取本初子午线对应的标准时间
        Instant now = Instant.now();
        System.out.println(now);

        // 添加时间偏移量
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        // 获取对应的毫秒数,1970-1-1开始的，类似 toTime()
        long l = now.toEpochMilli();
        System.out.println(l);

        // 获取给定毫秒数获取实例，类似 new Date(time);
        Instant instant = Instant.ofEpochMilli(1639225147502L);
        System.out.println(instant);


    }
}
